<?php defined('SYSPATH') or die(); ?>
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>多平台广告添加/修改</title>
    {template 'stourtravel/public/public_js'}
    {php echo Common::getCss('style.css,base.css,base_new.css'); }
    {php echo Common::getScript("uploadify/jquery.uploadify.min.js,product_add.js,choose.js,imageup.js,DatePicker/WdatePicker.js"); }

    <style>
        .set-text-xh {
            float: none;
        }

        .bz {
            color: #c5c5c5;
        }

        .radio_check {
            vertical-align: middle;
            margin: -2px 2px 0 0
        }

        .order-info-bar {
            height: 33px;
            background: #f1f9ff;
        }

        .order-info-bar .bt-bar {
            display: inline-block;
            color: #8fc0d6;
            height: 33px;
            line-height: 33px;
            padding: 0 20px;
            font-size: 13px;
        }
    </style>
</head>

<body>

    <table class="content-tab">
        <tr>
            <td width="119px" class="content-lt-td" valign="top">
                {template 'stourtravel/public/leftnav'}
                <!--右侧内容区-->
            </td>
            <td valign="top" class="content-rt-td ">
                <form method="post" name="product_frm" id="product_frm">
                    <div class="cfg-header-bar">
                        <a href="javascript:;" class="fr btn btn-primary radius mt-6 mr-10" onclick="window.location.reload()">刷新</a>
                    </div>
                    <div class="manage-nr mt-1">
                        <div class="product-add-div">
                            <ul class="info-item-block">
                                <li><span class="item-hd">模板：</span>
                                    <div class="item-bd"><span class="item-text ">{$kindname}</span></div>
                                </li>
                                <li><span class="item-hd">显示位置：</span>
                                    <div class="item-bd"><span class="item-text ">{$ads['position']}</span></div>
                                </li>
                                <li>
                                    <span class="item-hd">可使用产品：</span>
                                    <div class="item-bd">
                                        <div class="typeid_9999">
                                            <a href="javascript:;" class="btn btn-primary radius size-S ml-10 upload_item" data="{title:'上传图片','callback':'upload_image'}" title="上传图片">上传图片</a>
                                            <a href="javascript:;" class="btn btn-primary radius size-S ml-10" onclick="add_product.getaddproduct(this,'.attr-sel',1)" title="选择产品">选择产品</a>
                                        </div>
                                        <div class="apply-tab-wrap typeid_9999" style="display:block">

                                            <div class="apply-tab-block mt-10 pr-20">
                                                <table class="table table-border table-bordered" id="dlg_tb">
                                                    <tr>
                                                        <th width="50">排序</th>
                                                        <th width="150">产品编号</th>
                                                        <th>广告内容/产品类型</th>
                                                        <th>广告标题/产品名称</th>
                                                        <th>广告链接</th>
                                                        <th width="150">显示端</th>
                                                        <th width="150">管理</th>
                                                    </tr>
                                                </table>
                                                <div class="btn-block">
                                                    <div class="pm-btm-msg" id="page_info">

                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </li>
                            </ul>
                        </div>

                </form>
            </td>
        </tr>
    </table>

    <script>
        var client = <?php echo json_encode($client, JSON_UNESCAPED_UNICODE); ?>
        //图片上传
        $('.upload_item').click(function() {
            var data = eval('(' + $(this).attr('data') + ')');
            ST.Util.showBox(data.title, SITEURL + 'image/insert_view/iswater/0', 0, 0, null, null, document, {
                loadWindow: window,
                loadCallback: eval('(' + data.callback + ')')
            });

            function upload_image(result, bool) {
                if (result.data.length > 0) {
                    var data = [];
                    for (i = 0; i < result.data.length; i++) {
                        var temp = result.data[i].split('$$');
                        data.push(temp[0])
                    }
                    $.ajax({
                        type: "post",
                        url: SITEURL + "wxxcx/ajax_upload_save/",
                        dataType: 'json',
                        data: {
                            number: "{$ads['number']}",
                            prefix: "{$prefix}",
                            adsrc: data.join(',')
                        },
                        success: function(res) {
                            get_product_list();
                        }
                    });
                }
            }

        });
        add_product = {
            getsetmodeltype: function(dom, selector) {

                var models = [];
                $(selector + " input:hidden").each(function(index, obj) {

                    models.push($(obj).val());
                });
                models = models.join('_');
                CHOOSE.setSome("选择品类", {
                    loadWindow: window,
                    loadCallback: add_product.listgetsetmodeltype
                }, SITEURL + 'coupon/admin/coupon/dialog_setmodeltype?models=' + models + '&selector=' + encodeURI(selector), 1);
            },
            listgetsetmodeltype: function(result, bool) {
                var data = result.data;
                var html = '';
                $.each(data, function(index, model) {
                    html += '<span  style="display: inline-block; float: none"><s onclick="$(this).parent(\'span\').remove()"></s>' + model.modulename + '<input type="hidden" name="models[]" value="' + model.id + '"></span>';
                })
                $('.modules-sel').html(html)
            },
            getaddproduct: function() {
                var prefix = "{$prefix}";
                var params = {
                    loadCallback: add_product.listchooseProduct,
                    loadWindow: window
                };
                var url = SITEURL + 'wxxcx/add_product/number/{$ads["number"]}/prefix/' + prefix;
                ST.Util.showBox('指定产品', url, '900', '600', null, null, document, params);
            },
            listchooseProduct: function() {
                get_product_list(); //加载列表
            }
        };
        $(document).ready(function() {
            $('input[name=isnever]').click(function() {
                var val = $(this).val();
                if (val > 0) {
                    $('.isneverspan').show();
                } else {

                    $('.isneverspan').hide();
                }
            });
            $('input[name=typeid]').click(function() {
                var val = $(this).val();
                if (val == 1) {
                    $('.typeid_1').css('display', 'inline-block')
                    $('.typeid_9999').hide();
                } else if (val == 9999) {
                    $('.typeid_9999').each(function(index, obj) {
                        if ($(obj).hasClass('apply-tab-wrap')) {
                            $(obj).show()
                        } else {
                            $(obj).css('display', 'inline-block')
                        }
                    })
                    $('.typeid_1').hide();
                } else {
                    $('.typeid_1').hide();
                    $('.typeid_9999').hide();

                }


            })

            $('#amount0').blur(function() {
                var val = parseInt($(this).val());
                if (isNaN(val)) {
                    val = '';
                } else {
                    val = Math.abs(val);
                    if (val > 99999) {
                        val = 99999;
                    }
                }
                $(this).val(val);
            });

            $('#amount1').blur(function() {
                var val = parseFloat($(this).val());
                if (isNaN(val)) {
                    val = '';
                } else {
                    val = Math.abs(val);
                    if (val >= 10) {
                        val = 9.9;
                    }
                }
                $(this).val(val);
            });

            $('#samount').blur(function() {
                var val = parseInt($(this).val());
                if (isNaN(val)) {
                    val = '';
                } else {
                    val = Math.abs(val);
                    if (val >= 1000000) {
                        val = 999999;
                    }
                }
                $(this).val(val);
            });


        });
    </script>



    <!--    产品信息-->
    <script>
        function togStatus(obj) {
            get_product_list();
        }

        function get_product_list() {
            var prefix = "{$prefix}";
            var keyword = $("#search_input").val();
            var typeid = $('#search_typeid').val();
            $.ajax({
                type: 'get',
                dataType: 'json',
                data: {
                    page: 1,
                    keyword: keyword,
                    typeid: typeid
                },
                url: SITEURL + "wxxcx/pro_list/number/{$ads['number']}/prefix/" + prefix,
                success: function(result) {
                    genList(result);
                    $('.listen_blur').each(function() {
                        $(this).blur(function() {
                            Ext.Ajax.request({
                                url: SITEURL + "wxxcx/index/action/update",
                                method: "POST",
                                datatype: "JSON",
                                params: {
                                    id: $(this).parents('tr').attr('data-id'),
                                    field: $(this).attr('name'),
                                    val: $(this).val()
                                },
                                success: function(response, opts) {
                                    if (response.responseText == 'ok') {
                                        get_product_list();
                                    } else {
                                        ST.Util.showMsg("{__('norightmsg')}", 5, 1000);
                                    }
                                }
                            });
                        })

                    });
                }
            })
        }

        function loadProducts(page) {
            var prefix = "{$prefix}";
            var typeid = $('#search_typeid').val();
            var url = SITEURL + "wxxcx/pro_list/number/{$ads['number']}/prefix/" + prefix;
            var keyword = $("#search_input").val();
            $.ajax({
                type: "get",
                url: url,
                dataType: 'json',
                data: {
                    page: page,
                    keyword: keyword,
                    typeid: typeid
                },
                success: function(result, textStatus) {
                    genList(result);
                }
            });
        }


        function in_array(needle, haystack) {
            var len = haystack.length,
                bool = -1;
            for (var i = 0; i < len; i++) {
                if (haystack[i] == needle) {
                    bool = i;
                }
            }
            return bool;
        }

        function genList(result) {
            var html = '';
            for (var i in result.lists) {
                var row = result.lists[i];
                html += '<tr class="tb-item" data-id="' + row["id"] + '">' +
                    '<td><input type="text" class="row-edit-txt listen_blur" value="' + row["displayorder"] + '" name="displayorder"></td>' +
                    '<td>' + row['bh'] + '</td>' +
                    '<td>' + (row['typeid'] ? row['typename'] : '<a class="example-image-link"><img class="example-image" src="' + row['adsrc'] + '"></a>') + '</td>' +
                    '<td>' + (row['typeid'] ? row['protitle'] : '<input name="adname" class="input-text listen_blur" value="' + row['adname'] + '">') + '</td>' +
                    '<td>' + (row['typeid'] ? '--' : '<input name="adlink" class="input-text listen_blur" value="' + row['adlink'] + '">') + '</td><td>';
                for (var j = 0; j < client.length; j++) {
                    html += '<label class="radio-label" style="padding:0 5px;"><input type="checkbox" name="client[]" value="' + j + '" ' + (in_array(j, row['client_no'].split(',')) > -1 ? 'checked="checked"' : '') + '>' + client[j] + '&nbsp;</label>';
                }
                html += '</td><td><a class="delete" href="javascript:;" onclick="delRow(this,' + row.id + ')" >移除</a></td></tr>';
            }

            $("#dlg_tb .tb-item").remove();
            $("#dlg_tb").append(html);

            var pageHtml = ST.Util.page(result.pagesize, result.page, result.total, 5);
            $("#page_info").html(pageHtml);
            $("#page_info a").click(function() {
                var page = $(this).attr('page');
                loadProducts(page);
            });

            $('input[name="client[]"]').change(function() {
                var arr = new Array();
                $(this).parent().parent().find("input").each(function(i) {
                    if ($(this).attr('checked')) {
                        arr.push($(this).val())
                    }
                });
                Ext.Ajax.request({
                    url: SITEURL + "wxxcx/index/action/update",
                    method: "POST",
                    datatype: "JSON",
                    params: {
                        id: $(this).parents('tr').attr('data-id'),
                        field: 'client_no',
                        val: arr.join(",")
                    },
                    success: function(response, opts) {
                        if (response.responseText == 'ok') {
                            get_product_list();
                        } else {
                            ST.Util.showMsg("{__('norightmsg')}", 5, 1000);
                        }
                    }
                });
            });
        }

        function delRow(dom, id) {

            Ext.Ajax.request({
                url: SITEURL + "wxxcx/add_product/action/del",
                method: "POST",
                params: {
                    id: id
                },
                success: function(response, opts) {
                    var text = response.responseText;
                    if (text == 'ok') {
                        get_product_list();
                    } else {
                        ST.Util.showMsg("{__('norightmsg')}", 5, 1000);
                    }
                }
            });
        }



        function updateField(ele, id) {
            var value = Ext.get(ele).getValue();
            var field = 'displayorder';
            Ext.Ajax.request({
                url: SITEURL + "wxxcx/index/action/update",
                method: "POST",
                datatype: "JSON",
                params: {
                    id: id,
                    field: field,
                    val: value
                },
                success: function(response, opts) {
                    if (response.responseText == 'ok') {
                        get_product_list();
                    } else {
                        ST.Util.showMsg("{__('norightmsg')}", 5, 1000);
                    }
                }
            });


        }

        get_product_list();
    </script>




</body>

</html>